//acg模块网络请求

import 'package:tiktokadult/hj_model/acg/comic_chapters_model.dart';
import 'package:tiktokadult/hj_model/acg/search_index_model.dart';
import 'package:tiktokadult/hj_model/acg/tag_list_model.dart';
import 'package:tiktokadult/hj_model/home/module_detail_model.dart';
import 'package:tiktokadult/hj_model/list_base_model.dart';
import 'package:tiktokadult/hj_model/video_model.dart';
import 'package:tiktokadult/hj_page/user_center_page/model/liabrary_config_model.dart';
import 'package:tiktokadult/hj_utils/http_manager.dart';

import '../../hj_model/acg/CartoonMoreList.dart';
import '../../hj_model/cartoon_media_info.dart';
import '../../hj_model/media_content.dart';
import '../../hj_model/user/mine_video.dart';

class ACGService {
  //获取动漫详情 种类 1、动漫，2、漫画
  static Future<CartoonMediaInfo?> getMediaInfo(String id) async {
    final param = {'id': id};
    final result = await httpManager.fetchResponseByGET(
      '/media/info',
      param: param,
      jsonTransformation: (json) => CartoonMediaInfo.fromJson(json),
    );
    return result.isSuccess ? result.data : null;
  }

  // 获取动漫筛选分类
  static Future<VideoLibraryConfigModel> getMediaLibrary() async {
    final result = await httpManager.fetchResponseByGET('/media/library',
        param: {}, jsonTransformation: (json) => VideoLibraryConfigModel.fromJson(json));

    return result.data;
  }

  /// 获取标签数据
  static Future<CartoonMoreListModel?> fetchMediaByTagId(String tagid, {int page = 1, int size = 12, String? sort}) async {
    final param = {
      'tag': tagid,
      'pageNumber': page,
      'pageSize': size,
      'sortType': sort,
    }..removeWhere((key, value) => value == null);

    final result = await httpManager.fetchResponseByGET(
      '/media/listWithTag',
      param: param,
      jsonTransformation: (json) => CartoonMoreListModel.fromJson(json),
    );
    return result.isSuccess ? result.data : null;
  }

  ///动漫更多数据
  static Future<CartoonMoreListModel?> getMoreCartoonData(
    int pageNumber,
    int pageSize,
    int sortType, //排序 1最多播放，2、最新，3、 最多收藏 "
    String? tagID,
  ) async {
    final param = {
      'tagID': tagID,
      'pageNumber': pageNumber,
      'pageSize': pageSize,
      'sortType': sortType,
    };
    param.removeWhere((k, v) => v == null);
    final result = await httpManager.fetchResponseByGET(
      "/tag/media/list", //'/media/topic', // tag/media/list
      param: param,
      jsonTransformation: (json) => CartoonMoreListModel.fromJson(json),
    );
    return result.isSuccess ? result.data : null;
  }

  //加入书架
  static Future<bool> mediaBookshelfAdd(String id) async {
    final param = {'id': id};
    final result = await httpManager.fetchResponseByPOST(
      '/media_bookshelf/add',
      param: param,
    );
    return result.isSuccess;
  }

  /// 获取标签详情
  static Future<TagsBean?> fetchMediaTagInfo(String tagId) async {
    final param = {'id': tagId};
    final result =
        await httpManager.fetchResponseByGET('/media_tag/info', param: param, jsonTransformation: (json) => TagsBean.fromMap(json));
    return result.data;
  }

  //移除书架
  static Future<bool> mediaBookshelfDelete(String id) async {
    final param = {'id': id};
    final result = await httpManager.fetchResponseByPOST(
      '/media_bookshelf/del',
      param: param,
    );
    return result.isSuccess;
  }

  //acg热门标签 0-视频帖子标签 1-acg标签
  static Future<TagListModel?> acgHotTag({int page = 1, int size = 10}) async {
    final param = {'pageNumber': page, 'pageSize': size};
    final result = await httpManager.fetchResponseByGET(
      '/tag/list',
      param: param,
      jsonTransformation: (json) => TagListModel.fromJson(json),
    );
    return result.data;
  }

  //acg热门推荐 0-最新热播 1-本月最热 2-上月最热
  static Future<CartoonMoreListModel?> fetchAcgRecommend(
    int pageNumber,
    int pageSize, {
    String? mediaType = 'video',
    int? type = 0,
  }) async {
    final param = {
      'pageNumber': pageNumber,
      'pageSize': pageSize,
      'mediaType': mediaType,
      'type': type,
    };
    final result = await httpManager.fetchResponseByGET(
      '/media/hot',
      param: param,
      jsonTransformation: (json) => CartoonMoreListModel.fromJson(json),
    );
    return result.data;
  }

  //获取媒体书架列表 //排序 0、最新收藏 1.最近更新
  static Future<MineCollect<T>?> fetchAcgBooklib<T>(
    String type, {
    int page = 1,
    int size = 20,
  }) async {
    final param = {
      'pageNumber': page,
      'pageSize': size,
      'type': type,
    };
    final result = await httpManager.fetchResponseByGET(
      '/media_bookshelf/list',
      param: param,
      jsonTransformation: (json) => MineCollect<T>.fromJson(json),
    );
    return result.data;
  }

  static Future<MineCollect<T>?> fetchAcgBuyData<T>(
    int pageNumber,
    int pageSize, {
    String? mediaType = 'video',
  }) async {
    final param = {
      'pageNumber': pageNumber,
      'pageSize': pageSize,
      'mediaType': mediaType,
    };
    final result = await httpManager.fetchResponseByGET(
      '/media/my_buy',
      param: param,
      jsonTransformation: (json) => MineCollect<T>.fromJson(json),
    );
    return result.data;
  }

  //acg任务领取奖励
  static Future<ReceiveAwardModel?> acgTaskReceive(String taskId) async {
    final param = {'taskId': taskId};
    final result = await httpManager.fetchResponseByPOST(
      '/media_task/receive',
      param: param,
      jsonTransformation: (json) => ReceiveAwardModel.fromJson(json),
    );
    return result.data;
  }

  //acg日/周/人气等榜单 1-日榜 2-周榜 3-人气榜 4-热度榜 5-钻石榜 6-连载榜

  static Future<ListBaseModel<CartoonMediaInfo>?> acgRanking(
    int pageNumber,
    int pageSize, {
    String? mediaType = 'video', //video 动画 image 漫画 text 小说
    int? type = 1,
  }) async {
    final param = {
      'pageNumber': pageNumber,
      'pageSize': pageSize,
      'mediaType': mediaType,
      'type': type,
    };
    final result = await httpManager.fetchResponseByGET(
      '/media/ranking',
      param: param,
      jsonTransformation: (json) => ListBaseModel<CartoonMediaInfo>.fromJson(json),
    );
    return result.data;
  }

  //获取动漫内容章节列表 sortType： 0-正序 1-倒序

  static Future<ListBaseModel<ComicChapterInfo>?> mediaContentChapterList(String mediaId, int pageNumber, int pageSize,
      {int? sortType = 0}) async {
    final param = {
      'pageNumber': pageNumber,
      'pageSize': pageSize,
      'mediaId': mediaId,
      'sortType': sortType,
    };
    final result = await httpManager.fetchResponseByGET(
      '/media_content/list',
      param: param,
      jsonTransformation: (json) => ListBaseModel<ComicChapterInfo>.fromJson(json),
    );
    return result.data;
  }

  //动漫详情-推荐列表,相似作品
  static Future<ListBaseModel<CartoonMediaInfo>?> comicsRecommendList(
    int pageNumber,
    int pageSize, {
    String? mediaId, //0-视频帖子标签 1-acg标签
  }) async {
    final param = {
      'pageNumber': pageNumber,
      'pageSize': pageSize,
      'mediaId': mediaId,
    };
    final result = await httpManager.fetchResponseByGET(
      '/media/recommend',
      param: param,
      jsonTransformation: (json) => ListBaseModel<CartoonMediaInfo>.fromJson(json),
    );
    return result.data;
  }

  //动漫搜索数据 排序 1、精选，2、最新 ，3最热
  static Future<MediaSearchListModel?> librarySearch({int pageNumber = 1, int pageSize = 10, Map<String, dynamic>? filters}) async {
    final param = <String, dynamic>{
      'pageNumber': pageNumber,
      'pageSize': pageSize,
    }..addAll(filters ?? {});
    final result = await httpManager.fetchResponseByPOST(
      '/media/library/search',
      param: param,
      jsonTransformation: (json) => MediaSearchListModel.fromJson(json),
    );
    return result.data;
  }

  //kind：动漫片库搜索 1、动漫 2、漫画 3、小说
  //sortType：1-最新上架 2-最多观看
  static Future<MediaSearchListModel?> mediaSearch(
      {int page = 1, int size = 20, int? kind, String? tagName, int? sortType, String? keyword}) async {
    final param = {'pageNumber': page, 'pageSize': size, 'kind': kind, 'tagName': tagName, 'sortType': sortType, 'keyword': keyword};
    param.removeWhere((key, value) => value == null);
    final result = await httpManager.fetchResponseByGET(
      '/media/search',
      param: param,
      jsonTransformation: (json) => MediaSearchListModel.fromJson(json),
    );
    return result.isSuccess ? result.data : null;
  }

  //获取子集详情
  static Future<MediaContent?> getMediaDetailInfo({
    String? id,
    int? episodeNumber,
    String? mediaId,
  }) async {
    final para = {
      'id': id,
      'episodeNumber': episodeNumber,
      'mediaId': mediaId,
    };
    final result = await httpManager.fetchResponseByGET(
      '/media_content/info',
      param: para,
      jsonTransformation: (json) => MediaContent.fromJson(json),
    );
    return result.isSuccess ? result.data : null;
  }

  //获取动漫详情 0-acg类型专题，暂时只有这个
  static Future<AcgSectionModel?> getSectionList(
    int pageNumber,
    int pageSize, {
    int? type = 0,
  }) async {
    final param = {'pageNumber': pageNumber, 'pageSize': pageSize, 'type': 0};
    final result = await httpManager.fetchResponseByGET(
      '/section/list',
      param: param,
      jsonTransformation: (json) => AcgSectionModel.fromJson(json),
    );
    return result.data;
  }

  //获取动漫片库接口V2
  static Future<VideoLibraryConfigModel?> getMediaLibary() async {
    final result = await httpManager.fetchResponseByGET(
      '/media/library',
      param: {},
      jsonTransformation: (json) => VideoLibraryConfigModel.fromJson(json),
    );
    return result.data;
  }

  //获取动漫片库接口V2
  static Future<CartoonMoreListModel?> getMediaLibaryResult({
    String? mediaType,
    int? pageNumber = 1,
    int? pageSize = 15,
    String? payType,
    String? tagName,
    String? sortType,
  }) async {
    final param = {
      'mediaType': mediaType,
      'pageNumber': pageNumber,
      'pageSize': pageSize,
      'payType': payType,
      'tagName': tagName,
      'sortType': sortType,
    };
    final result = await httpManager.fetchResponseByGET(
      '/media/library/search',
      param: param,
      jsonTransformation: (json) => CartoonMoreListModel.fromJson(json),
    );
    return result.data;
  }

  //acg搜索页热门tag
  static Future<ListBaseModel<HotTag>?> getAcgHotTag(
    int pageNumber,
    int pageSize, {
    int kind = 1, //0-视频帖子标签 1-acg标签
  }) async {
    final param = {
      'pageNumber': pageNumber,
      'pageSize': pageSize,
      'kind': kind,
    };
    final result = await httpManager.fetchResponseByGET(
      '/search/tags',
      param: param,
      jsonTransformation: (json) => ListBaseModel<HotTag>.fromJson(json),
    );
    return result.data;
  }

  //acg获取小说列表
  static Future<CartoonMoreListModel?> getNovelList(
    int pageNumber,
    int pageSize, {
    String? sectionId,
    int sortType = 1, //1-最新更新 2-热播推荐 3-最多点赞 4-最多观看
  }) async {
    final param = {
      'pageNumber': pageNumber,
      'pageSize': pageSize,
      'sectionId': sectionId,
      'sortType': sortType,
    }..removeWhere((key, value) => value == null);

    final result = await httpManager.fetchResponseByGET(
      '/media/novel',
      param: param,
      jsonTransformation: (json) => CartoonMoreListModel.fromJson(json),
    );
    return result.data;
  }

  ///动漫更多数据
  static Future<CartoonMoreListModel?> getPreferences(
    int pageNumber,
    int pageSize, {
    String sId = '',
    int? sortType = 0, //排序 1-最新 2-最多观看 3-最多喜欢（收藏）
    int? type = 1, //0-默认专题这些页面获取 1-首页获取(猜你喜欢)
  }) async {
    final param = {
      'pageNumber': pageNumber,
      'pageSize': pageSize,
      'sId': sId,
      'sortType': sortType,
      'type': type,
    };
    param.removeWhere((key, value) => value == null);
    final result = await httpManager.fetchResponseByGET(
      '/media/topic',
      param: param,
      jsonTransformation: (json) => CartoonMoreListModel.fromJson(json),
    );
    return result.isSuccess ? result.data : null;
  }
}
